package com.tlb.springboot.starter.rocketmq.factory;

import com.aliyun.openservices.ons.api.impl.rocketmq.OnsClientRPCHook;
import com.tlb.springboot.starter.rocketmq.config.RocketMqProperties;
import com.tlb.springboot.starter.rocketmq.impl.RocketMQProducer;
import com.tlb.springboot.starter.rocketmq.util.AliAddressUtil;

/**
 * @author yuaneg 2018/9/4
 */
public class ProducerFactory {

    private RocketMqProperties properties;

    public ProducerFactory(RocketMqProperties properties) {
        this.properties = properties;
    }

    public RocketMQProducer createProducer(String producerGroup) {
        if (null == properties.getSessionCredentials()) {
            return new RocketMQProducer(producerGroup);
        } else {
            return new RocketMQProducer(AliAddressUtil.getNamespace(properties.getNameServer()), producerGroup,
                new OnsClientRPCHook(properties.getSessionCredentials()));
        }
    }

}
